#include <stdio.h>
#include <stdlib.h>
#include <time.h> // For clock() and time() functions

#define N 100000 // 10万个随机数

// --- 辅助函数 ---
void swap(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

// --- 快速排序 (Quick Sort) ---
int partition(int arr[], int low, int high)
{
    int pivot = arr[high];
    int i = (low - 1);
    for (int j = low; j <= high - 1; j++)
    {
        if (arr[j] < pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}

void quickSort(int arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

// --- 归并排序 (Merge Sort) ---
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;

    int *L = (int *)malloc(n1 * sizeof(int)); // 动态分配内存
    int *R = (int *)malloc(n2 * sizeof(int)); // 动态分配内存

    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];

    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }

    free(L); // 释放动态分配的内存
    free(R); // 释放动态分配的内存
}

void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {
        int m = l + (r - l) / 2;
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
        merge(arr, l, m, r);
    }
}

// --- 堆排序 (Heap Sort) ---
void heapify(int arr[], int n, int i)
{
    int largest = i;
    int left = 2 * i + 1;
    int right = 2 * i + 2;

    if (left < n && arr[left] > arr[largest])
        largest = left;

    if (right < n && arr[right] > arr[largest])
        largest = right;

    if (largest != i)
    {
        swap(&arr[i], &arr[largest]);
        heapify(arr, n, largest);
    }
}

void heapSort(int arr[], int n)
{
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);

    for (int i = n - 1; i > 0; i--)
    {
        swap(&arr[0], &arr[i]);
        heapify(arr, i, 0);
    }
}

// --- 主函数 ---
int main()
{
    int arr[N];
    int temp_arr[N]; // 用于每次排序前恢复原始随机数

    // 生成随机数
    srand(time(NULL)); // 使用当前时间作为随机数种子
    for (int i = 0; i < N; i++)
    {
        arr[i] = rand() % N; // 0 到 N-1 之间的随机数
    }

    clock_t start_time, end_time;
    double elapsed_time_ms;

    // --- 快速排序 ---
    printf("C Language Quick Sort:\n");
    for (int i = 0; i < N; i++)
        temp_arr[i] = arr[i]; // 复制原始随机数
    start_time = clock();
    quickSort(temp_arr, 0, N - 1);
    end_time = clock();
    elapsed_time_ms = ((double)(end_time - start_time)) / CLOCKS_PER_SEC * 1000.0;
    printf("Time taken: %.2f milliseconds\n", elapsed_time_ms);

    // --- 归并排序 ---
    printf("C Language Merge Sort:\n");
    for (int i = 0; i < N; i++)
        temp_arr[i] = arr[i]; // 复制原始随机数
    start_time = clock();
    mergeSort(temp_arr, 0, N - 1);
    end_time = clock();
    elapsed_time_ms = ((double)(end_time - start_time)) / CLOCKS_PER_SEC * 1000.0;
    printf("Time taken: %.2f milliseconds\n", elapsed_time_ms);

    // --- 堆排序 ---
    printf("C Language Heap Sort:\n");
    for (int i = 0; i < N; i++)
        temp_arr[i] = arr[i]; // 复制原始随机数
    start_time = clock();
    heapSort(temp_arr, N);
    end_time = clock();
    elapsed_time_ms = ((double)(end_time - start_time)) / CLOCKS_PER_SEC * 1000.0;
    printf("Time taken: %.2f milliseconds\n", elapsed_time_ms);

    return 0;
}